<template>
	<div>
		<van-nav-bar
		  title="注册"
			left-text="返回"
			 @click-left="handleLeftClick"
		/>
		<van-cell-group>
		  <van-field 
				v-model="addForm.username" 
				label="账号" 
				placeholder="请输入用户名" 
				clearable
			/>
			<van-field 
				v-model="addForm.password" 
				type="password" 
				label="密码" 
				placeholder="请输入密码" 
				clearable
			/>
			<van-field
				v-model="addForm.password1" 
				type="password" 
				label="确认密码" 
				placeholder="请输入确认密码" 
				clearable
			/>
		</van-cell-group>
		<div style="text-align: center;padding: 15px;">
			<van-button type="warning" @click="handleRegister">提交</van-button>
		</div>
	</div>
</template>

<script>
	import {register} from '@/api/user-api.js'
	export default{
		methods:{
			handleLeftClick(){
				this.$router.history.go(-1)
			},
			async handleRegister(){
				if(this.addForm.username == ''){
					this.$notify({ type: 'danger', message: '账号不可以为空' })
					return
				}
				if(this.addForm.password == ''){
					this.$notify({ type: 'danger', message: '密码不可以为空' })
					return
				}
				if(this.addForm.password1 == ''){
					this.$notify({ type: 'danger', message: '确认密码不可为空' })
					return
				}
				if(this.addForm.password != this.addForm.password1){
					this.$notify({ type: 'danger', message: '两次密码不一致' })
					return
				}
				let res = await register(this.addForm.username,this.addForm.password)
				if(res.data.code == 200){
					this.$notify({ type: 'succcess', message: '注册成功，请登录' })
					this.$router.push('/login')
				}
			}
		},
		data(){
			return {
				addForm:{
					username:'',
					password:'',
					password1:''
				}
			}
		},
	}
</script>

<style>
</style>
